<template>
  <div>
    <el-form class="f" :model="form" ref="form" label-width="120px" label-position="right" :rules="rules">
      <v-form-title title="修改绑定手机"/>
      <el-form-item label="原手机号码">
        {{userInfo.phone}}
      </el-form-item>
      <el-form-item label="原手机号验证码" prop="oldPhoneVerificateCode">
        <el-input class="line-input" maxlength="6" v-model="form.oldPhoneVerificateCode" placeholder="请输入原手机验证码">
          <v-message-code slot="suffix" @click.native="$refs.messageOldCode.start" ref="messageOldCode" class="edit-el-intput-suffix"
                          :phoneNum="userInfo.phone"></v-message-code>
        </el-input>
      </el-form-item>

      <el-form-item label="新手机号码" prop="newPhoneNumber">
        <el-input class="line-input" maxlength="11" v-model="form.newPhoneNumber" placeholder="请输入手机号码"></el-input>
      </el-form-item>
      <el-form-item label="新手机验证码" prop="newPhoneVerificateCode">
        <el-input class="line-input" maxlength="6" v-model="form.newPhoneVerificateCode" placeholder="请输入手机号码">
          <v-message-code slot="suffix" @click.native="$refs.messageNewCode.start" ref="messageNewCode" class="edit-el-intput-suffix"
                          :phoneNum="form.newPhoneNumber"></v-message-code>
        </el-input>
      </el-form-item>
    </el-form>
    <div class="opera">
      <el-button type="primary" @click="checkForm">确认修改</el-button>
    </div>
  </div>
</template>

<script lang="ts">
  import { Vue, Component, Prop } from 'vue-property-decorator'
  import vFormTitle from '@/components/form-title'
  import vMessageCode from '@/components/message-code'

  @Component({
    components: {
      vFormTitle, vMessageCode
    }
  })
  export default class Base extends Vue {
    @Prop() userInfo

    form = {
      oldPhoneVerificateCode: '',
      newPhoneNumber: '',
      newPhoneVerificateCode: ''
    }
    rules = {
      newPhoneNumber: [
        {required: true, message: '请输入手机号码', trigger: 'blur'},
        {
          pattern: /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/,
          message: '手机号码错误',
          trigger: 'blur'
        }
      ],
      newPhoneVerificateCode: [
        {required: true, message: '请输入原手机验证码', trigger: 'blur'}
      ],
      oldPhoneVerificateCode: [
        {required: true, message: '请输入新手机验证码', trigger: 'blur'}
      ]
    }

    checkForm () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.submitForm()
        }
      })
    }

    async submitForm () {
      let f = {
        ...this.form,
        oldPhoneNumber: this.userInfo.phone
      }
      let res: AjaxResponse = await  this.$axios.put('g/gateway/channelprovider/changephone', f)
      this.$utils.tips(res)
      if (res.responseCode === 200) {
        this.$router.push('/login')
      }
    }

  }
</script>

<style lang="less" scoped>
  .f {
    width: 1180px;
    margin: 0 auto;
    padding-top: 60px;
  }

  .opera {
    text-align: center;
    margin-top: 100px;
  }
</style>
